<?php 
class Region extends CS_Controller
{
    private $data = array();

    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('region_model', 'region');
    }

    /**
     * 根据ajax反馈的父id，返回所有子集 （json格式）
     */
    public function select_children($parent_id = 0)
    {
        $childrenData = array();
        if ($parent_id) {
            $childrenData = $this->region->children_of($parent_id);
        }
        echo json_encode($childrenData);
        exit;
    }

    public function autocompleteCity()
    {
        $result = $this->region->findByAutocomplete(2); //1.代表省；2.代表城市；3.代表区或县
        $cityArray = $result->result_array();
        echo json_encode($cityArray);
        exit;
    }

    /*
     * 获取所有数据
     * */
    public function select()
    {
        $childrenData = $this->region->all_region();
        echo json_encode($childrenData);
        exit;
    }

    /**
     * 从 sfzArea.js 这个文件传回 json数据,里面有每个城市对应的区域编码，即身份证的前几问编码
     */
    public function regionCodeUpdate()
    {
        $region = json_decode($this->input->post('region'));
        $one = '0';
        $province = $region->$one;
        $result = $this->region->findByParentId(1)->result();
        foreach ($province as $key => $value) {
            $str = '0_' . $key;
            $arr = explode('|', $value);
            $arr[0] = str_replace(array('省', '市', '区'), array(''), $arr[0]);
            $len = strlen($arr[0]);
            foreach ($result as $k => $val) {
                if ($arr[0] == mb_substr($val->region_name, 0, $len)) {
                    $this->data[] = array('region_id' => $val->region_id, 'region_code' => $arr[1]);
                    $this->updateCities($val->region_id, $region, $str, $key);
                    unset($result[$k]);
                }
            }
        }
        $this->region->updateRegionCode($this->data);
        exit;
    }

    /**
     * 查找出地区  递归函数
     * @param $parentId
     * @param $region
     * @param $str
     */
    public function updateCities($parentId, $region, $str)
    {
        $result = $this->region->findByParentId($parentId)->result();
        if (!isset($region->$str)) {
            return;
        }
        $regionData = $region->$str;
        if (!empty($regionData) && is_array($regionData)) {
            foreach ($regionData as $key => $value) {
                $arr = explode('|', $value);
                $arr[0] = str_replace(array('省', '市', '区', '县'), array(''), $arr[0]);
                foreach ($result as $k => $val) {
                    $name = str_replace(array('省', '市', '区', '县'), array(''), $val->region_name);
                    $len = mb_strlen($name);
                    if ($name == mb_substr($arr[0], 0, $len)) {
                        $this->data[] = array('region_id' => $val->region_id, 'region_name' => $val->region_name, 'region_code' => $arr[1]);
                        unset($result[$k]);
                        if ($val->region_type <= 3) {
                            $this->updateCities($val->region_id, $region, $str . '_' . $key);
                            break;
                        }
                    }
                }
            }
        }
    }
}